<?php
namespace User\Controller;
use Think\Controller;


class PayController extends Controller {
	
//首款
public function first_paragraph(){
 $order = D("order");
 $sn = date('Ymdhis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);  //随机数 作为订单号
 $arr = array('first_sn' => $sn);

 if($_POST){
 	session('user_id' , $_POST['user_id']); //跳转页面使用
 	$id = $order -> where("order_sn = {$_POST['order_sn']}") -> save($arr);
 	if($id){
 		echo json_encode($sn);
 	}
 }


}
//尾款

public function  tail_paragraph(){
	$order = D("order");
 	$sn = date('Ymdhis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);  //随机数 作为订单号
 	$arr = array('tail_sn' => $sn);

 	if($_POST){
 		session('user_id' , $_POST['user_id']); //跳转页面使用
 		$id = $order -> where("order_sn = {$_POST['order_sn']}") -> save($arr);
 		if($id){
 			echo json_encode($sn);
 		}
 	}
}

//全额
public function in_full(){
	$order = D("order");
 	$sn = date('Ymdhis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);  //随机数 作为订单号
 	$arr = array('in_full_sn' => $sn);

 	if($_POST){
 		session('user_id' , $_POST['user_id']); //跳转页面使用
 		$id = $order -> where("order_sn = {$_POST['order_sn']}") -> save($arr);
 		if($id){
 			echo json_encode($sn);
 		}
 	}
}



//订单支付回调方法
public function payapi(){
	 $total_amount =$_GET['total_amount'];//支付金额
	 $out_trade_no =$_GET['out_trade_no'];//支付订单号
	 $timestamp = $_GET['timestamp'];//支付时间
	 $trade_no = $_GET['trade_no']."1";//支付宝订单号
	 $order = D('order');
	 $total_amounts = $order ->where("first_sn = {$out_trade_no} or tail_sn ={$out_trade_no} or in_full_sn ={$out_trade_no}") -> field('total_amount') ->find();//订单总额
	 $has_pay_amount = $order ->where("first_sn = {$out_trade_no} or tail_sn ={$out_trade_no} or in_full_sn ={$out_trade_no}") -> field('has_pay_amount')->find();//已经支付的金额
	 $total_amounts  = $total_amounts['total_amount'];
	 $has_pay_amount  = $has_pay_amount['has_pay_amount'];
	 
	 if($has_pay_amount > 0){// 尾款操作
	 	//echo "wei kuan 1";
	 	$zong = ($total_amount + $has_pay_amount);
	 	$arr = array(
	 			
	 			'tail_je' => $total_amount,
			 	'tail_time' =>$timestamp,
			 	'tail_jy_sn' =>$trade_no,
			 	'has_pay_amount' => $zong
			 );
	 	$id = $order ->where("tail_sn = {$out_trade_no}") ->save($arr);
	 	
		if( $id){
			$arr = array(
			 				'pay_s_jj' => 3  //尾款支付
					 );
					$id = $order ->where("tail_sn = {$out_trade_no}") ->save($arr);//更改订单状态
					if($id){
						$this -> success('尾款支付成功,调转我的订单页面', U("user/user"),2);
					}
		}


	 }else{//首款操作或者全额操作
			 
			 if($total_amount == $total_amounts){// 全额支付
			 		$arr = array(
			 		
			 			'in_full_je' => $total_amount,
			 			'in_full_time' =>$timestamp,
			 			'in_full_jy_sn' =>$trade_no,
					 	'has_pay_amount' => $total_amount
					 );
					$id = $order ->where("in_full_sn = {$out_trade_no}") ->save($arr);
					if($id){
							$arr = array(
				 				'pay_s_jj' => 2  //支付全部
						 );
						$id = $order ->where("in_full_sn = {$out_trade_no}") ->save($arr);//更改订单状态
						if($id){
							$this -> success('全额支付成功,调转我的订单页面', U("user/user"),2);
						}
					}

			 }else{//首款支付
			 			
						 $arr = array(
						 	
						 	'first_je' => $total_amount,
			 				'first_time' =>$timestamp,
			 				'first_jy_sn' =>$trade_no,
						 	'has_pay_amount' => $total_amount
						 );
						$id = $order ->where("first_sn = {$out_trade_no}") ->save($arr);
						
						if($id){//更新订单状态
							
								$arr = array(
						 				'pay_s_jj' => 1  //支付了首款
								 );
								$id2 = $order ->where("first_sn = {$out_trade_no}") ->save($arr);//更改订单状态
								if($id2){
									$this -> success('首款支付成功,调转我的订单页面', U("user/user"),2);
									
								}

				
			}
		}
}
}



	public function ss(){
			$id = session('user_id');
											$this -> success('调转我的订单页面', U("user/user#/", array('order' => $id)));
		}

	public function wxpayapi(){ 
			 // $arr =$_POST['out_trade_no'];
			 // echo $arr."aaa";  
			 $arr = str_split($_POST['time_end']);
			 $timestamp = $arr[0].$arr[1].$arr[2].$arr[3]."-".$arr[4].$arr[5]."-".$arr[6].$arr[7]." ".$arr[8].$arr[9].":".$arr[10].$arr[11].":".$arr[12].$arr[13];
			 $total_amount =$_POST['total_fee']*0.01;//支付金额
			 $out_trade_no =$_POST['out_trade_no'];//支付订单号
			 $timestamp =$timestamp; //支付时间
			 $trade_no = $_POST['transaction_id']."2";//微信订单号
			 $order = D('order');
			 $total_amounts = $order ->where("first_sn = {$out_trade_no} or tail_sn ={$out_trade_no} or in_full_sn ={$out_trade_no}") -> field('total_amount') ->find();//订单总额
			 $has_pay_amount = $order ->where("first_sn = {$out_trade_no} or tail_sn ={$out_trade_no} or in_full_sn ={$out_trade_no}") -> field('has_pay_amount')->find();//已经支付的金额
			 $total_amounts  = $total_amounts['total_amount'];
			 $has_pay_amount  = $has_pay_amount['has_pay_amount'];	

			 if($has_pay_amount > 0){// 尾款操作
			 	//echo "wei kuan 1";
			 	$zong = ($total_amount + $has_pay_amount);
			 	$arr = array(
			 			
			 			'tail_je' => $total_amount,
					 	'tail_time' =>$timestamp,
					 	'tail_jy_sn' =>$trade_no,
					 	'has_pay_amount' => $zong
					 ); 
			 	$id = $order ->where("tail_sn = {$out_trade_no}") ->save($arr);			 	
				if( $id){
					$arr = array(
					 				'pay_s_jj' => 3  //尾款支付
					 				 //微信支付
							 );
							$id = $order ->where("tail_sn = {$out_trade_no}") ->save($arr);//更改订单状态
							if($id){
								echo 'TAIL_SUCCESS';
							}
				}
			 }else{//首款操作或者全额操作	

					 if($total_amount == $total_amounts){// 全额支付
					 		$arr = array(
					 			
					 			'in_full_je' => $total_amount,
					 			'in_full_time' =>$timestamp,
					 			'in_full_jy_sn' =>$trade_no,
							 	'has_pay_amount' => $total_amount
							 );
					 		
							$id = $order ->where("in_full_sn = {$out_trade_no}") ->save($arr);
							if($id){
									$arr = array(
						 				'pay_s_jj' => 2  //支付全部
						 				 //微信支付
								 );
								$id = $order ->where("in_full_sn = {$out_trade_no}") ->save($arr);//更改订单状态
								if($id){
									echo'FULL_SUCCESS';
								}
							}
					 }else{//首款支付					 			
								 $arr = array(
								 	
								 	'first_je' => $total_amount,
					 				'first_time' =>$timestamp,
					 				'first_jy_sn' =>$trade_no,
								 	'has_pay_amount' => $total_amount
								 );
								$id = $order ->where("first_sn = {$out_trade_no}") ->save($arr);							
								if($id){//更新订单状态									
										$arr = array(
								 				'pay_s_jj' => 1 //支付了首款
										 		//微信支付	
										 );
										$id2 = $order ->where("first_sn = {$out_trade_no}") ->save($arr);//更改订单状态
										if($id2){
											echo'FIRST_SUCCESS';											
										}						
					}
				}
		}
		}



}
